﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.JCI.Common.Utility;
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Dal;

namespace Microsoft.JCI.Bll
{
    public class MasterDataBll:BllBase
    {
         private   string CacheName = "GetAllCompany";
        public MasterDataBll() 
        {
            _cacheName = DeclareHelper.CacheKeysConstant.MasterDataAllCacheKey;
        }

        #region Company
        public List<SysCompanyEntity> GetAllCompanyFromCache()
        {

            if (base.ExistsCacheData(CacheName))
            {
                return GetCacheData(CacheName) as List<SysCompanyEntity>;
            }
            else
            {
                SysCompanyDal dal = new SysCompanyDal();
                List<SysCompanyEntity> list = dal.GetAll<SysCompanyEntity>("");

                AddCache(CacheName, list);

                return list;
            }

        }

        public List<SysCompanyEntity> GetCompanyListFromCache(Enumerator.CompanyType type)
        { 
            if (base.ExistsCacheData(CacheName))
            {
                List<SysCompanyEntity> list = null;
                list = GetCacheData(CacheName) as List<SysCompanyEntity>;

                return list.FindAll(delegate(SysCompanyEntity e) { return e.Type == (int)type; });
            }
            else
            {
                SysCompanyDal dal = new SysCompanyDal();
                List<SysCompanyEntity> list = dal.GetAll<SysCompanyEntity>("");

                AddCache(CacheName, list);

                return list.FindAll(delegate(SysCompanyEntity e) { return e.Type == (int)type; });
            }
        }

        public List<SysCompanyEntity> GetCompanyListFromCacheByProvince( string ProvinceCode)
        {
            if (base.ExistsCacheData(CacheName))
            {
                List<SysCompanyEntity> list = null;
                list = GetCacheData(CacheName) as List<SysCompanyEntity>;

                return list.FindAll(delegate(SysCompanyEntity e) { return e.Province == ProvinceCode; });
            }
            else
            {
                SysCompanyDal dal = new SysCompanyDal();
                List<SysCompanyEntity> list = dal.GetAll<SysCompanyEntity>("");

                AddCache(CacheName, list);

                return list.FindAll(delegate(SysCompanyEntity e) { return e.Province == ProvinceCode; });
            }
        }

        public List<SysCompanyEntity> GetCompanyListFromCacheByParentCode(string ParentCode)
        {
            if (base.ExistsCacheData(CacheName))
            {
                List<SysCompanyEntity> list = null;
                list = GetCacheData(CacheName) as List<SysCompanyEntity>;

                return list.FindAll(delegate(SysCompanyEntity e) { return e.ParentCompanyCode == ParentCode; });
            }
            else
            {
                SysCompanyDal dal = new SysCompanyDal();
                List<SysCompanyEntity> list = dal.GetAll<SysCompanyEntity>("");

                AddCache(CacheName, list);

                return list.FindAll(delegate(SysCompanyEntity e) { return e.ParentCompanyCode == ParentCode; });
            }
        }

        public SysCompanyEntity GetCompanyFromCache(string Code)
        {
            if (base.ExistsCacheData(CacheName))
            {
                List<SysCompanyEntity> list = null;
                list = GetCacheData(CacheName) as List<SysCompanyEntity>;

                return list.Find(delegate(SysCompanyEntity e) { return e.CompanyCode == Code; });
            }
            else
            {
                SysCompanyDal dal = new SysCompanyDal();
                List<SysCompanyEntity> list = dal.GetAll<SysCompanyEntity>("");

                AddCache(CacheName, list);

                return list.Find(delegate(SysCompanyEntity e) { return e.CompanyCode == Code; });
            }
        }
        #endregion
    }
}
